package com.qf.mybatis01.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
/*
    封装sqlSession工具类
 */
public class MybatisUtils {
    public static SqlSession getSqlSession() throws IOException {
        //把mybatis-config.xml转换成输入流对象
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        //创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        //利用SqlSessionFactory对象 可以创建 SqlSession对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

        //核心对象 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        return sqlSession;
    }
}
